#include "stdafx.h"
#include "ImmStuff.h"
#include "CompAndCandWnd.h"
#include "KeyHandler.h"
#include "ImmData.h"
#include "StatusClass.h"

BOOL WINAPI ImeInquire(LPIMEINFO lpIMEInfo, LPTSTR lpszUIClass, LPCTSTR lpszOption)
{
	lpIMEInfo->dwPrivateDataSize = sizeof(UIEXTRA);
    lpIMEInfo->fdwProperty = IME_PROP_KBD_CHAR_FIRST |
#ifdef _UNICODE
                             IME_PROP_UNICODE |
#endif
                             IME_PROP_SPECIAL_UI | IME_PROP_END_UNLOAD;

    lpIMEInfo->fdwConversionCaps = IME_CMODE_FULLSHAPE | IME_CMODE_NATIVE;
    lpIMEInfo->fdwSentenceCaps = IME_SMODE_NONE;
    lpIMEInfo->fdwUICaps = UI_CAP_2700;
	lpIMEInfo->fdwSCSCaps = 0;
    lpIMEInfo->fdwSelectCaps = SELECT_CAP_CONVERSION;
    _tcscpy(lpszUIClass,UiClassName);
    return TRUE;
}

BOOL WINAPI ImeConfigure(HKL hKL,HWND hWnd, DWORD dwMode, LPVOID lpData)
{
	DialogBox(ImmStuff::hInst,MAKEINTRESOURCE(IDD_CONFIG),hWnd,ConfigDialogProc);
	InvalidateRect(hWnd,NULL,FALSE);
	return TRUE;
}

DWORD WINAPI ImeConversionList(HIMC hIMC,LPCTSTR lpSource,LPCANDIDATELIST lpCandList,DWORD dwBufLen,UINT uFlag)
{
    return 0;
}

BOOL WINAPI ImeDestroy(UINT uForce)
{
    return FALSE;
}

LRESULT WINAPI ImeEscape(HIMC hIMC,UINT uSubFunc,LPVOID lpData)
{
	return FALSE;
}

BOOL WINAPI ImeProcessKey(HIMC hIMC, UINT vKey, LPARAM lparam, CONST LPBYTE lpbKeyState)
{
	KeyHandler handler(hIMC,vKey,NULL,lparam,lpbKeyState);
	return handler.Process();
}

BOOL WINAPI ImeSelect(HIMC hIMC, BOOL fSelect)
{
    if (fSelect)
        ImmStuff::UpdateIndicIcon(hIMC);
	ImmData imm_data(hIMC);
	imm_data.Initial();	
    return TRUE;
}

BOOL WINAPI ImeSetActiveContext(HIMC hIMC,BOOL fFlag)
{
	ImmStuff::UpdateIndicIcon(hIMC);
    return TRUE;
}

UINT WINAPI ImeToAsciiEx (UINT uVKey,UINT lparam,CONST LPBYTE lpbKeyState,LPDWORD lpdwTransKey,UINT fuState,HIMC hIMC) 
{	
	ImmStuff::uNumTransKey = 0;
	KeyHandler handler(hIMC,uVKey,lpdwTransKey,lparam,lpbKeyState);
	handler.HandleEx();
    return ImmStuff::uNumTransKey;
}

BOOL WINAPI NotifyIME(HIMC hIMC,DWORD dwAction,DWORD dwIndex,DWORD dwValue)
{
    BOOL bRet = FALSE;
	ImmData* lpImmData = new ImmData(hIMC);
    switch(dwAction)
    {
	case NI_CONTEXTUPDATED:
		switch (dwValue)
		{
		case IMC_SETOPENSTATUS:
			if(!lpImmData->GetImmOpenState() && lpImmData->IsCompStr())
				lpImmData->MakeResultString(NULL,FALSE);
			delete lpImmData;
			ImmStuff::UpdateIndicIcon(hIMC);
			bRet = TRUE;
			break;
		default:
			break;
		}
		break;			
	default:
		break;
    }
    return bRet;
}

BOOL WINAPI ImeRegisterWord(LPCTSTR lpRead, DWORD dw, LPCTSTR lpStr)
{
    return FALSE;
}

BOOL WINAPI ImeUnregisterWord(LPCTSTR lpRead, DWORD dw, LPCTSTR lpStr)
{
    return FALSE;
}

UINT WINAPI ImeGetRegisterWordStyle(UINT nItem, LPSTYLEBUF lp)
{
	return 0;
}

UINT WINAPI ImeEnumRegisterWord(REGISTERWORDENUMPROC lpfn, LPCTSTR lpRead, DWORD dw, LPCTSTR lpStr, LPVOID lpData)
{
	return 0;
}

BOOL WINAPI ImeSetCompositionString(HIMC hIMC, DWORD dwIndex, LPCVOID lpComp, DWORD dwComp, LPCVOID lpRead, DWORD dwRead)
{
    return FALSE;
}